# @Author: Eric Ito
# @Date: 1/29/2009
# @Name: Project Euler Problem 34


"""
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the
factorial of their digits.
"""

from util import fact

def main():
    LOOKUP = {}
    NUMBERS = []
    UPPER = 0
    for i in range(10):
        LOOKUP[i] = fact(i)
        UPPER += LOOKUP[i]

    for i in range(3,UPPER+1):
        total = 0
        for num in str(i):
            total += LOOKUP[int(num)]
        if total == i:
            NUMBERS.append(i)
    print sum(NUMBERS)

if __name__ == "__main__":
    main()